package com.ruoyi.framework.redis;

import com.ruoyi.framework.config.AliossConfig;
import com.ruoyi.framework.utils.OssUtils;
import com.ruoyi.framework.utils.SmsUtils;
import net.dreamlu.mica.core.spring.SpringContextUtil;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.utils.RandomType;
import net.dreamlu.mica.redis.cache.CacheKey;
import net.dreamlu.mica.redis.cache.MicaRedisCache;

import java.util.Map;


/**
 * 缓存工具栏
 *
 * @author dyun
 */
public class CacheTool {

    private static MicaRedisCache micaRedisCache;

    public static MicaRedisCache getMicaRedisCache() {
        if (micaRedisCache == null) {
            micaRedisCache = SpringContextUtil.getBean(MicaRedisCache.class);
        }
        return micaRedisCache;
    }


    /**
     * policy缓存方法
     *
     * @return oss前端签名数据
     */
    public static Map<String, String> getPolicy() {
        return getMicaRedisCache().get(OssUtils.getOssKey(), AliossConfig::getPolicy);
    }

    /**
     * 获取短信验证码并缓存
     *
     * @param uuid 验证码类型唯一标识
     * @return 验证码
     */
    public static String getSmsCode(String uuid) {
        String code = $.random(6, RandomType.INT);
        CacheKey smsKey = SmsUtils.getSmsKey(uuid);
        getMicaRedisCache().set(smsKey, code);
        return code;
    }

}
